package com.gangwangchina.android.readsms.config;

import android.R.bool;
import android.R.string;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.database.Cursor;
import android.os.RemoteException;
import android.provider.ContactsContract;
import android.util.Log;

import com.gangwangchina.android.readsms.ui.Settings;
import com.shoushuo.android.tts.ITts;
import com.shoushuo.android.tts.ITtsCallback;

public class Global {

	private String TAG = "Global";

	private static Global m_Global;

	private int mReadCoount;

	private boolean bIsReadSmsServerRunning;

	private boolean bIsInited;

	public Config config;

	private Context m_ctxContext = null;

	public final String ACTION_STOP_READ = "com.gangwangchina.android.readsms.STOPREAD";

	public boolean ACTION_STOP_READ_SENDED = false;

	protected BroadcastReceiver m_BroadcastStopRead = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			Global.Instance().stopReadIfSpeaking();
		}
	};

	public static Global Instance() {
		if (m_Global == null) {
			m_Global = new Global();
		}

		return m_Global;
	}

	private Global() {
		this.m_Global = null;
		this.bIsInited = false;

		bIsReadSmsServerRunning = false;
		mReadCoount = 1;
	}

	public boolean Init(Context ctx) {
		if (this.bIsInited) {
			return true;
		}

		if (ctx == null) {
			return false;
		}

		m_ctxContext = ctx;

		this.config = new Config(Settings.getAutoRead(this.m_ctxContext),
				Settings.getReadRecv(this.m_ctxContext),
				Settings.getAutoStart(this.m_ctxContext),
				Settings.getDownAStopRead(this.m_ctxContext),
				Settings.getReadCount(this.m_ctxContext));

		IntentFilter filter = new IntentFilter();
		filter.addAction(ACTION_STOP_READ);
		this.m_ctxContext.registerReceiver(m_BroadcastStopRead, filter);
		this.bIsInited = true;
		return true;
	}

	public boolean Finit() {

		this.m_ctxContext.unregisterReceiver(m_BroadcastStopRead);
		this.bIsInited = false;

		return true;
	}

	public Context getContext() {
		return this.m_ctxContext;
	}

	public final ITtsCallback mCallback = new ITtsCallback.Stub() {
		@Override
		public void speakCompleted() throws RemoteException {
			// ttsHandler.sendEmptyMessage(0);
		}
	};

	private ITts ttsService = null;

	public void setItts(ITts ttsService) {
		this.ttsService = ttsService;
	}

	public void ReadSmsContext(String number, String body) {
		if (ttsService == null || !this.config.bAutoRead) {
			Log.w(TAG, "Fun:ReadSmsContext --> ttsService is null");
			return;
		}

		String recvPersonString = getPersonNameByNumber(number);
		if (recvPersonString == null
				|| recvPersonString.equalsIgnoreCase("") == true) {
			recvPersonString = number;
		}

		ACTION_STOP_READ_SENDED = false;
		String contextString = "接收到来自";
		contextString += recvPersonString;
		contextString += "的短信内容为：";

		if (this.config.bReadrecv) {
			try {
				this.ttsService.speak(contextString, 1);
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		// mReadCoount = 2;
		for (int i = 0; i < Settings.getReadCount(this.m_ctxContext); i++) {

			try {
				this.ttsService.speak(body, 1);
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public void stopReadIfSpeaking() {
		try {
			if (this.ttsService.isSpeaking()) {
				this.ttsService.stop();
			}
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void setReadSmsServerRunning(boolean bIsReadSmsServerRunning) {
		this.bIsReadSmsServerRunning = bIsReadSmsServerRunning;
	}

	public boolean getReadSmsServerRunning() {
		return bIsReadSmsServerRunning;
	}

	public class Config {
		public boolean bAutoRead = true;

		public boolean bReadrecv = true;

		public boolean bAutoStartServer = true;

		public boolean bDownstopread = true;

		public int readcount = 1;

		public Config(boolean autoread, boolean readrecv,
				boolean autostartserver, boolean downstopread, int count) {
			this.bAutoRead = autoread;
			this.bReadrecv = readrecv;
			this.bAutoStartServer = autostartserver;
			this.bDownstopread = downstopread;
			this.readcount = count;
		}
	}

	private String getPersonNameByNumber(String phoneNum) {

		if( phoneNum.startsWith("+86"))
		{
			phoneNum = phoneNum.substring(3);
		}
		
		String name = null;
		ContentResolver cr = this.m_ctxContext.getContentResolver();
		Cursor pCur = cr.query(
				ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
				ContactsContract.CommonDataKinds.Phone.NUMBER + " = ?",
				new String[] { phoneNum }, null);
		if (pCur.moveToFirst()) {
			name = pCur
					.getString(pCur
							.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
			pCur.close();
		}
		return name;
	}
	
	public boolean isSpeaking() {
		try {
			return this.ttsService.isSpeaking();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return false;
	}
	
	public void stopSpeaking()
	{
		try {
			if( this.ttsService.isSpeaking() )
			{
				try {
					this.ttsService.stop();
				} catch (RemoteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
